<template>
	<view class="flex-col space-y-50 section_2">
	  <view class="flex-col space-y-21">
	    <button class="foot" v-if="type == 'phone'" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" color="#DE182B">
			获取微信手机号
		</button>
		<button class="foot" v-else color="#DE182B" @click="getUserInfoFun">
			获取微信用户信息
		</button>
	    <view>
	      <text class="font_1">登录代表您已同意易知谷ERP</text>
	      <text class="font_2">《用户协议》</text>
	      <text class="font_1">和</text>
	      <text class="font_2">《隐私政策》</text>
	    </view>
	  </view>
	</view>
</template>

<script>
	import WXBizDataCrypt from '@/common/js/common.js'
	import {userInfoPhone,getCode,getCodephone} from '@/api/index.js'
	export default {
		props:['type'],
		data() {
			return {
				openid:'',
				unionid:'',
				session_key:'',
				jsCode:'',
				checkState:true,
				userInfo:{},
				rdsession:''
			};
		},
		onLoad(option) {
			
		},
		created() {
			this.getCodeFun();
		},
		methods:{
			getUserInfoFun() {
				let that = this
				uni.getUserProfile({
					desc: "获取用户信息",
					success(e) {
						let userInfo = that.$userInfo();
						userInfo.wcNickname = e.userInfo.nickName;
						userInfo.headUrl = e.userInfo.avatarUrl;
						that.$store.commit('storeLogin',userInfo);
						that.closePage();
					},
					fail(e) {
						uni.showToast({
							icon: "none",
							title: "已取消登录"
						})
					}
				})
			},
			async onGetPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  
					//拒绝授权后弹出一些提示  
					uni.showToast({
						icon: "none",
						title: "已取消登录"
					})
				} else { //允许授权  
					if (!this.checkState) return uni.showToast({
						icon: "none",
						title: "请阅读并勾选用户协议"
					})
					let rdsession = uni.getStorageSync('rdsession');
					// e.detail.encryptedData //加密的用户信息  
					// e.detail.iv //加密算法的初始向量  时要用到 
					// let pc = new WXBizDataCrypt(appInfos['appid'], appInfos['session_key']); 
					// let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
					let info = await userInfoPhone({iv:e.detail.iv,encryptedData:e.detail.encryptedData,rdsession:rdsession})
					if (info.code == 200) {
						this.$store.commit('storeLogin', info.data)
						uni.setStorageSync('token',info.msg);
						this.closePage();
					}
				}
			},
			getCodeFun(){
				if(this.$userInfo().id) return
				uni.login({
					success: (res) => {
						if (res.code) { 
							this.jsCode = res.code 
							getCodephone({code:res.code}).then(re => {
								if(re.code == 200)
									this.rdsession = re.data;
									uni.setStorageSync('rdsession',this.rdsession)
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			closePage(){
				this.$emit('closePage')
			}
		}
	}
</script>

<style lang="scss" scoped>
.section_2 {
  padding: 72rpx 20rpx 26rpx 27rpx;
  background-color: #ffffff;
  border-radius: 4rpx;
  width: 500rpx;
}
.space-y-50 > view:not(:first-child),
.space-y-50 > text:not(:first-child),
.space-y-50 > image:not(:first-child) {
  margin-top: 50rpx;
}
.text {
  align-self: center;
  color: #141414;
  font-size: 34rpx;
  font-family: Microsoft YaHei;
  line-height: 34rpx;
}
.space-y-21 > view:not(:first-child),
.space-y-21 > text:not(:first-child),
.space-y-21 > image:not(:first-child) {
  margin-top: 21rpx;
}
.section_3 {
  margin-left: 23rpx;
  margin-right: 30rpx;
  padding: 26rpx 61rpx 26rpx 65rpx;
  background-color: #37c637;
  border-radius: 4rpx;
}
.space-x-12 > view:not(:first-child),
.space-x-12 > text:not(:first-child),
.space-x-12 > image:not(:first-child) {
  margin-left: 12rpx;
}
.image {
  flex-shrink: 0;
  width: 36rpx;
  height: 28rpx;
}
.text_2 {
  color: #ffffff;
  font-size: 28rpx;
  font-family: Microsoft YaHei;
  line-height: 28rpx;
}
.font_1 {
  font-size: 24rpx;
  font-family: Microsoft YaHei;
  line-height: 40rpx;
  color: #999999;
}
.font_2 {
  font-size: 24rpx;
  font-family: Microsoft YaHei;
  line-height: 40rpx;
  color: #5372fc;
}

.foot {
	background-color: #37c637;
	color: #ffffff;
	margin-bottom: 40rpx;
}
</style>